package com.zg.ktcomwan

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.zg.core.ext.logI
import com.zg.core.ext.toast
import com.zg.ktcomwan.data.RetrofitUtils
import com.zg.ktcomwan.ext.alert
import com.zg.ktcomwan.model.ArticleBean
import com.zg.ktcomwan.repository.ArticleRepositroty
import com.zg.ktcomwan.vm.ArticleViewModel
import com.zg.ktcomwan.vm.NetViewModel
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.coroutines.*
import java.text.SimpleDateFormat
import java.util.*

/***
 * https://blog.csdn.net/yuzhiqiang_1993/article/details/101012090
 * 目标
 * 1.显示首页数据、项目、广场三个tab就行
 * android retrofit+协程
 * 现在的基础框架代码
 * https://www.jianshu.com/p/6a33f367a56d
 * --------------------------------------------------------------------
 * https://blog.csdn.net/sange77/article/details/102575852 MVP
 * https://blog.csdn.net/sange77/article/details/103959389 MVVM
 * https://blog.csdn.net/sange77/article/details/102536792
 */
class MainActivity : AppCompatActivity() {
    private lateinit var netViewModel: NetViewModel
    private lateinit var articleViewModel: ArticleViewModel
    private lateinit var list: List<ArticleBean>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        /*创建viewmodel*/
        netViewModel = ViewModelProvider(this).get(NetViewModel::class.java)

        mHello.setOnClickListener {
            netViewModel.getHomeArticleList()
            articleViewModel.getArticleList()
        }
        Log.d("zxd", "当前线程：${Thread.currentThread().name}")
        netViewModel.articles.observe(this, Observer {
            Log.i("zxd", "netViewModel: ")
            Log.d("zxd", "netViewModel当前线程：${Thread.currentThread().name}")
            mHello.text = "数据大小：" + it.size
        })

        articleViewModel = ViewModelProvider(this).get(ArticleViewModel::class.java)
        articleViewModel.article.observe(this, Observer {
            Log.i("zxd", "articleViewModel: ")
            Log.d("zxd", "articleViewModel当前线程：${Thread.currentThread().name}")
            mHello.text = "集合长度：" + it.size
        })

        val coroutineScope = CoroutineScope(Job() + Dispatchers.Default)
        list = ArrayList()
        mHello1.setOnClickListener {
            coroutineScope.launch(Dispatchers.Main) {
                Log.d("zxd", "当前线程1：${Thread.currentThread().name}")
                list = getSuspendHomeArticle1()
                Log.d("zxd", "当前线程3：${Thread.currentThread().name}")
                mHello1.text = "长度=" + list.size

                val newList1: List<ArticleBean> = getSuspendHomeArticle1()
                Log.d("zxd", "打印newList1：${newList1.size}")
            }
        }

        initTest()

        showDialog.setOnClickListener {
            lifecycleScope.launch {
                val alert = alert("Warning!", "Do you know?")
                log("my choice is $alert")
            }
        }

        downloadPdf.setOnClickListener {
            toast(111)
            lifecycleScope.launch {
                val lists = RetrofitUtils.service.getProjectListTest(1, 367).data.datas
                logI("pdf size:${lists.size}")
            }
        }
    }

    private suspend fun getSuspendHomeArticle() {
        withContext(Dispatchers.IO) {
            Log.d("zxd", "当前线程2：${Thread.currentThread().name}")
            var articleList = ArticleRepositroty().getArticleList(0)
            list = articleList.data.datas
        }
    }

    suspend fun getSuspendHomeArticle1() = withContext(Dispatchers.IO) {
        Log.d("zxd", "当前线程2：${Thread.currentThread().name}")
        var articleList = ArticleRepositroty().getArticleList(0)
        return@withContext articleList.data.datas
    }


    val dateFormat = SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
    val now = { dateFormat.format(Date(System.currentTimeMillis())) }
    fun log(msg: Any?) = println("${now()} [${Thread.currentThread().name}] $msg")

    private fun initTest() {
        test1.setOnClickListener { GlobalScope.launch { test1() } }
        test2.setOnClickListener { GlobalScope.launch { test2_1() } }
        test3.setOnClickListener { GlobalScope.launch { test3() } }
    }

    suspend fun test1() {
        val launch = GlobalScope.launch {
            log("666")
        }
        launch.join()
        log("111")
    }

    suspend fun test2() {
        log(1)
        val job = GlobalScope.launch(start = CoroutineStart.LAZY) {
            log(2)
        }
        log(3)
//        job.start()//134-2
        job.join()//13-2-4
        log(4)
        Thread.sleep(2000)
    }

    suspend fun test2_1() {
        log(1)
        val async = GlobalScope.async(start = CoroutineStart.LAZY) {
            log(2)
        }
        log(3)
        async.await()//13-2-4
        log(4)
    }

    suspend fun test3() {
        log(1)
        val job = GlobalScope.launch(start = CoroutineStart.ATOMIC) {
            log(2)
            delay(1000)
            log(3)
        }
        job.cancel()
        log(4)
        job.join()
    }

}